#include <stdarg.h>
#include "std/vga.h"
#include "std/debug.h"

#ifdef TEST_DBG_WIN32
#include <stdio.h>
void go32_die_endless(void) {}
#endif

static void krnl_dbg_output(uint lvl, const char *fmt, va_list args)
{
	int i;
	char buf[256];

	if (lvl > DBG_LEVEL)
		return;

	buf[0] = 'L';
	buf[1] = '0' + lvl;
	buf[2] = ':';
	buf[3] = ' ';

	i = vsprintf(buf + 4, fmt, args);

#ifdef TEST_DBG_WIN32
	puts(buf);
#else
	vga_puts(buf);
#endif
}

void krnl_dbg_output1(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	krnl_dbg_output(DBG_LEVEL1, fmt, args);
	va_end(args);
}

void krnl_dbg_output2(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	krnl_dbg_output(DBG_LEVEL2, fmt, args);
	va_end(args);
}

void krnl_dbg_output3(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	krnl_dbg_output(DBG_LEVEL3, fmt, args);
	va_end(args);
}

void bug(const char *fmt, ...)
{
	va_list args;
	char buf[256];

	buf[0] = 'B';
	buf[1] = 'U';
	buf[2] = 'G';
	buf[3] = ':';
	buf[4] = ' ';

	va_start(args, fmt);
	vsprintf(buf + 5, fmt, args);

#ifdef TEST_DBG_WIN32
	puts(buf);
#else
	vga_puts(buf);
#endif

	go32_die_endless();
	va_end(args);
}

void _kassert(const char *str, const char *fn, uint ln)
{
	krnl_dbg_output1("viskrnl assertion failed: %s, file %s, line %u\n",
		str, fn, ln);
	go32_die_endless();
}

/* EOF */
